/******************************************************************************
*                                                                             *
*        **********                                                           *
*       ************                                                          *
*      ***        ***                                                         *
*     ***    ++    ***                                                        *
*     ***   +  +   ***                      CHIPCON                           *
*     ***   +                                                                 *
*     ***   +  +   ***                                                        *
*     ***    ++    ***                                                        *
*      ***        ***                                                         *
*       ************                                                          *
*        **********                                                           *
*                                                                             *
*******************************************************************************

Filename:     putchar.c
Target:       cc2430
Author:       efu
Revised:      16/12-2005
Revision:     1.0
******************************************************************************/
#include "hal.h"
#include "RF04EB.h"
#include "myglobals.h"

//-----------------------------------------------------------------------------
// See hal.h for a description of this function.
//-----------------------------------------------------------------------------

static char ringbuf[RBUFSIZE];
volatile UINT16 wind;
volatile UINT16 rind;
static volatile UINT16 anz;
volatile UINT8 tx_idle;

// TX Interrupt Pegelgesteuert, d.H. muss manuell auf 0 gesetzt werden

#ifdef __SDCC__
void uart0_tx_IRQ(void) __interrupt (UTX0_VECTOR)
#else
#pragma vector=UTX0_VECTOR
__interrupt void uart0_tx_IRQ(void)
#endif
{
  if (anz > 0) {
       anz--;
       U0DBUF = ringbuf[rind++];
       if (rind >= RBUFSIZE) rind = 0;
  }
else {
     tx_idle = 1;
     }
INT_SETFLAG(INUM_UTX0,INT_CLR);
}

void putbyte(char c) {
static volatile unsigned int i16;
while (anz >= RBUFSIZE) {
  }	
// sio irq aus 
INT_ENABLE(INUM_UTX0, INT_OFF);
//INT_GLOBAL_ENABLE(INT_OFF);
ringbuf[wind++] = c;
if (wind >= RBUFSIZE) wind = 0;
anz++;
if (tx_idle == 1) { // force IRQ call
  tx_idle =0;
  anz--;
  INT_SETFLAG(INUM_UTX0,INT_CLR);
  U0DBUF = ringbuf[rind++];
  if (rind >= RBUFSIZE) rind = 0;
  } // idle == 1
// sio irq an
INT_ENABLE(INUM_UTX0, INT_ON);
}


void putbyte_x(char c) {
//volatile INT16 i;
      while (!(UTX0IF))
#ifdef __SDCC__
      _asm
      nop
      _endasm;
#else
  asm (" nop");
#endif
      UTX0IF = 0;
      U0DBUF = c;       /* output CR  */
}



#ifdef __SDCC__
void putchar(char c) {
#else
int putchar(int c) {
#endif
//volatile INT16 i;
   if (c == '\n') putbyte(0x0d);       /* output CR  */
   putbyte(c);
//for(i=0; i< 1000; i++) ;
}

void init_uart(UINT32 baud) {
   wind=0;
   rind=0;
   anz=0;
   tx_idle=1;

   UART_SETUP(0,baud,HIGH_STOP);
//INT_SETFLAG(INUM_UTX0, INT_SET);
//   INT_ENABLE(INUM_UTX0, INT_OFF);
   INT_SETFLAG(INUM_UTX0, INT_CLR);
   INT_ENABLE(INUM_UTX0, INT_ON);
   INT_SETFLAG(INUM_URX0, INT_CLR);
   INT_ENABLE(INUM_URX0, INT_ON);
   U0CSR |= UART_ENABLE_RECEIVE ;
 //  U0CSR.MODE = 1;
}
